<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/7/30 0030
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller\config;


use app\admin\model\SysConfigTabs;
use app\admin\validate\CheckConfig;
use app\common\controller\AdminBaseController;
use app\common\service\FormBuilder as Form;
use think\Exception;
use think\facade\Route;


class Sysconfigtab extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model=new SysConfigTabs();
    }


    /**
     * @return string|\think\response\Json
     * @throws \FormBuilder\exception\FormBuilderException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:新增配置分类
     */
    public function addConfigTab(){
        if (IS_AJAX) {
            $data = $this->request->post();
           // dd($data);
            try {
                $checkUser = new CheckConfig();
                if (!$checkUser->scene("ConfigTab")->check($data)){
                    return parent::JsonReturn($checkUser->getError(), 0);
                }
                $res=$this->model->where(['eng_title' => "" . $data['eng_title'] . ""])->select()->toArray();
                if ($res){
                    return parent::JsonReturn("当前配置分类的英文名已存在", 0);
                }
                return $this->model->doAll($data);

            } catch (Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }

        $f = [];
        $f[] = Form::input('title', '配置分类名')->required("分类名必填")->placeholder("请输入配置分类名");
        $f[] = Form::input('eng_title', '分类英文')->required("分类英文必填")->placeholder("请输入配置分类英文")->info("配置分类的英文名称");
        $f[] = Form::radio('status', '状态', 1)->options([['label' => '正常', 'value' => 1], ['label' => '禁止', 'value' => 0]]);
        $f[] = Form::hidden("__token__", token());
        $f[] = Form::textarea("beizhu","备注");
        $form = Form::make_post_form('添加配置分类', $f, Route::buildUrl('addConfigTab'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


    /**
     * @return string|\think\response\Json
     * @throws \FormBuilder\exception\FormBuilderException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:
     */
    public function update(){
        if (IS_AJAX) {
            $data = $this->request->post();
            try {
                $checkUser = new CheckConfig();
                if (!$checkUser->scene("ConfigTab")->check($data)){
                    return parent::JsonReturn($checkUser->getError(), 0);
                }
                $res=$this->model->where(['eng_title' => "" . $data['eng_title'] . ""])->where("id","<>",$data['id'])->select()->toArray();
                if ($res){
                    return parent::JsonReturn("当前配置分类的英文名已存在", 0);
                }
                return $this->model->doAll($data);

            } catch (Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }
        $id = $this->request->param("id/d",0);//父id
        $info = $this->model->where("id",$id)->find()->toArray();
        if (!$info) return $this->failed("数据错误");
        $f = [];
        $f[] = Form::input('title', '配置分类名',$info['title'])->required("分类名必填")->placeholder("请输入配置分类名");
        $f[] = Form::input('eng_title', '分类英文',$info['eng_title'])->required("分类英文必填")->placeholder("请输入配置分类英文")->info("配置分类的英文名称");
        $f[] = Form::radio('status', '状态',$info['status'])->options([['label' => '正常', 'value' => 1], ['label' => '禁止', 'value' => 0]]);
        $f[] = Form::hidden("__token__", token());
        $f[] = Form::hidden("id", $info['id']);
        $f[] = Form::textarea("beizhu","备注",$info['beizhu']);
        $form = Form::make_post_form('添加配置分类', $f, Route::buildUrl('update'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


}